package TCPClient;

import java.util.Vector;

import messangerTypes.*;

@SuppressWarnings("serial")
public class TCPClientBaseUI extends javax.swing.JFrame implements Runnable {
    
    public void run ()
    {
    }

    private TCPClient parent;
    private Vector <User> userList;
    /**
     * Creates new form TCPClientBaseUI
     */
    public TCPClientBaseUI( TCPClient parent) {
    	this.parent = parent;
        initComponents();
        userList = null;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        usernameTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        passwordField = new javax.swing.JPasswordField();
        loginButton = new javax.swing.JToggleButton();
        connectButton = new javax.swing.JButton();
        userComboBox = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setText("Username:");

        usernameTextField.setText("jTextField1");

        jLabel2.setText("Password:");

        passwordField.setText("jPasswordField1");

        loginButton.setText("Login");
        loginButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                loginButtonMouseReleased(evt);
            }
        });

        connectButton.setText("Connect");
        connectButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                connectButtonMouseReleased(evt);
            }
        });

        userComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] {}));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(107, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(connectButton)
                            .addComponent(userComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jLabel2)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(passwordField)
                                    .addContainerGap())
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(usernameTextField)
                                    .addContainerGap()))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(loginButton)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(userComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(usernameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(connectButton))
                .addGap(17, 17, 17)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(loginButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();

    }// </editor-fold>                        

    private void connectButtonMouseReleased(java.awt.event.MouseEvent evt) {                                            
        // TODO add your handling code here:
        System.out.println("connectButton released");
        try
        {
        User user = userList.get(userComboBox.getSelectedIndex());
        ConnectRequest cr = new ConnectRequest(user, parent.getUser());
        parent.processMessage(cr);
        }
        catch (Exception e)
        {
        	//move on
        }
    }                                           

    private void loginButtonMouseReleased(java.awt.event.MouseEvent evt) {                                          
        
        if(loginButton.isSelected() && !(this.loginButton.getText().endsWith("out")))
        {
        	String[] names = this.usernameTextField.getText().split(" ", 2);
        	
        	if(names.length == 2)
        	{
            	//construct the user object
            	User user = new User (names[0], names[1], 0, new String(passwordField.getPassword()));
            	//then construct the connect request
            	ConnectRequest cr = new ConnectRequest(user, true);
            	this.parent.processMessage(cr);
            	this.loginButton.setText("Logout");
        	}
        	else
        	{
        		loginButton.setSelected(false);
        	}
        }
        else
        {
        	this.dispose();
        	System.exit(0);
        }
    }
    
    public void setLoggedOut()
    {
    	this.loginButton.setText("Login");
    	loginButton.setSelected(false);
    }
    
    public void clearFields ()
    {
        usernameTextField.setText(null);
        passwordField.setText(null);
    }
    
    public void addUsers (Vector <User> list)
    {
    	userList = list;
    	
    	this.userComboBox.removeAllItems();
    	
    	for(int i = 0; i < list.size(); i++)
    		this.userComboBox.addItem(list.get(i));
    }

    private void formWindowClosing(java.awt.event.WindowEvent evt) {                                   
        // TODO add your handling code here:
        System.out.println("Window Closing");
    }                                  

    // Variables declaration - do not modify                     
    private javax.swing.JButton connectButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JToggleButton loginButton;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JComboBox userComboBox;
    private javax.swing.JTextField usernameTextField;
    // End of variables declaration                   
}
